package com.nami.idgenerator.config;

import com.github.yitter.contract.IdGeneratorOptions;
import com.github.yitter.idgen.YitIdHelper;
import com.nami.idgenerator.properties.YitterProperties;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;



/**
 * @author lbc
 */
@Slf4j
@EnableConfigurationProperties(YitterProperties.class)
@AutoConfiguration
public class YitterIdGeneratorConfig {

    @Autowired
    private YitterProperties properties;

    @PostConstruct
    public void init() {
        startGeneratorOption();
        log.info("[yitter-init] current machineId ==> {}", properties.getMachineId());
    }

    public void startGeneratorOption() {
        if (null == properties.getMachineId()) {
            throw new NullPointerException("yitter machine id was null");
        }
        IdGeneratorOptions options = new IdGeneratorOptions(properties.getMachineId());

        YitIdHelper.setIdGenerator(options);
    }

}
